<template>
  <el-card>
    <el-select style="width: 300px;" placeholder="选择站点名称" v-model="name" filterable>
      <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.name" />
    </el-select>
  </el-card>

  <el-card style="margin-top: 20px;">
    <el-radio-group size="large" v-model="radio" @change="handleChange">
      <el-radio-button :label="`全部(${allList})`" :value="0" />
      <el-radio-button :label="`空闲中(${checkCount(1)})`" :value="1" />
      <el-radio-button :label="`充电中(${checkCount(2)})`" :value="2" />
      <el-radio-button :label="`连接中(${checkCount(3)})`" :value="3" />
      <el-radio-button :label="`排队中(${checkCount(4)})`" :value="4" />
      <el-radio-button :label="`已预约(${checkCount(5)})`" :value="5" />
      <el-radio-button :label="`故障/离线(${checkCount(6)})`" :value="6" />
    </el-radio-group>
  </el-card>

  <el-card style="margin-top: 20px;">
    <el-row :gutter="20">
      <el-col :span="6" v-for="item in dataListCopy" :key="item.id">
        <div class="item">
          <div class="imginfo">
            <div class="pic">
              <p v-if="item.status === 1">空闲中</p>
              <p v-if="item.status === 2">充电中</p>
              <p v-if="item.status === 3">连接中</p>
              <p v-if="item.status === 4">排队中</p>
              <p v-if="item.status === 5">已预约</p>
              <p v-if="item.status === 6">故障/离线</p>
              <img :src="item.status === 1 ? free : (item.status === 6 ? outline : ing)" alt="图片" width="100px">
              <p v-if="item.status === 2">{{ item.percent }}</p>
              <p v-else>0</p>
            </div>

            <div class="info">
              <h2> {{ item.id }}</h2>
              <hr>
              <p>{{ item.voltage }}</p>
              <p>{{ item.current }}</p>
              <p>{{ item.power }}</p>
              <p>{{ item.tem }}</p>
            </div>
          </div>
          <div class="btn">
            <div class="divder"></div>
            <div class="content">
              <p>暂无预警</p>
              <div>
                <el-button size="small">维保记录</el-button>
                <el-popover placement="right" :width="300" trigger="click" >
                  <template #reference>
                    <el-button size="small" type="primary" class="mr">使用记录</el-button>
                  </template>
                  <h3 class="timeline" style="font-size: 20px; font-weight: bold;margin-bottom: 15px; padding-left: 20px;">使用记录</h3>
                  <el-timeline style="max-width: 600px; padding: 0 20px;" >
                    <el-timeline-item :timestamp="j.time" v-for="j in item.record" :key="j.time" :hollow="true"
                      type="primary">
                      {{ j.msg }}
                    </el-timeline-item>

                  </el-timeline>
                </el-popover>
              </div>

            </div>
          </div>
        </div>
      </el-col>
    </el-row>
  </el-card>
</template>

<script setup lang="ts">
import free from '@/assets/free.png'
import outline from '@/assets/outline.png'
import ing from '@/assets/ing.png'
import { currentListApi } from '@/api/chargingstation'
import { computed, onMounted, ref, watch } from 'vue'

const radio = ref(0)
const name = ref('')
const options = ref<any>([])
const dataList = ref<any>([])
const allList = computed(() => {
  return checkCount(1) + checkCount(2) + checkCount(3) + checkCount(4) + checkCount(5) + checkCount(6)
})
const loadData = async () => {
  const { data } = await currentListApi()
  options.value = data
  dataList.value = data[0].list//原始数据
  dataListCopy.value = data[0].list//列表渲染
}

onMounted(() => {
  loadData()
})

const checkCount = (num: number) => {
  return dataList.value.filter((item: any) => item.status === num).length
}

const dataListCopy = ref<any>([])
const handleChange = () => {
  dataListCopy.value = dataList.value

  if (radio.value != 0) {
    dataListCopy.value = dataListCopy.value.filter((item: any) => item.status === radio.value)
  }
}

watch(name, () => {
  const res = options.value.filter((item: any) => item.name === name.value)
  dataList.value = res[0].list
  dataListCopy.value = res[0].list
  radio.value = 0
})

</script>


<style scoped lang="less">
.item {
  padding: 20px;
  border-radius: 10px 10px 0 0;
  background-color: rgb(247, 251, 254);
  margin-top: 20px;

  .imginfo {
    display: flex;
    justify-content: center;
    justify-items: center;

    .pic {
      color: rgb(90, 90, 90);
      justify-items: center;
      justify-content: center;

      img {
        padding: 10px 0;
      }
    }

    .info {
      color: rgb(135, 135, 135);
      margin-left: 50px;

      h2 {
        font-weight: bold;
        font-size: 18px;
      }

      p {
        margin: 15px 0;
      }
    }
  }

  .btn {
    .divder {
      background-color: rgba(101, 101, 101, 0.1);
      height: 1px;
      width: 100%;

    }

    .content {
      color: rgb(135, 135, 135);
      display: flex;
      justify-content: space-between;
      margin-top: 10px;
    }
  }
}

.el-popover{
  padding: 40px;
}
</style>